package com.fjzzct.report.controller;

import com.fjzzct.report.dao.TicketCheckDao;
import com.fjzzct.report.domain.ComicCheck;
import com.fjzzct.report.domain.PagerEntity;
import com.fjzzct.report.utils.AjaxFormat;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by cwb on 2015/10/29.
 */
@Controller
@RequestMapping(value = "/ticketcheck")
public class TicketCheckController extends BaseController {
    Gson gson = new Gson();
    @Autowired
    private TicketCheckDao ticketCheckDao;
    AjaxFormat af = new AjaxFormat();

    @RequestMapping(value = {"/index", ""})
    public String TicketCheckCheck() {
        return "TicketCheck/Index";
    }

    @RequestMapping(value = "/list")
    public
    @ResponseBody
    String GetList(int page, int rows) {
        PagerEntity pagerEntity = ticketCheckDao.GetTestList(page, rows);
        return gson.toJson(pagerEntity);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public
    @ResponseBody
    String Add(ComicCheck comicCheck, HttpServletRequest request) {
        String Ip = request.getRemoteAddr();
        SetDefaultValue(comicCheck);
        SetCreateInfo(comicCheck);
        comicCheck.setCreateIP(Ip);
        if (ticketCheckDao.Add(comicCheck)) {
            af.SetResult(AjaxFormat.ResultEnum.success);
            af.SetResultDesc("新增成功");
        } else {
            af.SetResult(AjaxFormat.ResultEnum.fail);
            af.SetResultDesc("新增失败");
        }
        return gson.toJson(af);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public
    @ResponseBody
    String Edit(ComicCheck comicCheck) {
        SetDefaultValue(comicCheck);
        if (ticketCheckDao.Edit(comicCheck)) {
            af.SetResult(AjaxFormat.ResultEnum.success);
            af.SetResultDesc("修改成功");
        } else {
            af.SetResult(AjaxFormat.ResultEnum.fail);
            af.SetResultDesc("修改失败");
        }
        return gson.toJson(af);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    String Delete(ComicCheck comicCheck) {
        SetDefaultValue(comicCheck);
        if (ticketCheckDao.Delete(comicCheck)) {
            af.SetResult(AjaxFormat.ResultEnum.success);
            af.SetResultDesc("删除成功");
        } else {
            af.SetResult(AjaxFormat.ResultEnum.fail);
            af.SetResultDesc("删除失败");
        }
        return gson.toJson(af);
    }
}
